﻿using System;
using System.Windows.Forms;
using Argos.Scheduling;

namespace Argos.Windows.Forms
{
	public partial class WeekdaySelector: UserControl
	{
		public WeekdaySelector()
		{
			InitializeComponent();
		}

		private WeekdayList _SelectedDays;

		public WeekdayList SelectedDays
		{
			get
			{
				if (null == _SelectedDays)
				{
					_SelectedDays = new WeekdayList();
					_SelectedDays.ItemAdded += new EventHandler<ItemEventArgs<DayOfWeek>>(SelectedDays_ItemAdded);
					_SelectedDays.ItemRemoved += new EventHandler<ItemEventArgs<DayOfWeek>>(SelectedDays_ItemRemoved);
					_SelectedDays.Cleared += new EventHandler<EventArgs>(SelectedDays_Cleared);
				}
				return _SelectedDays;
			}
		}



		private void SelectedDays_ItemAdded(object sender, ItemEventArgs<DayOfWeek> e)
		{
			this.HandleDay(e.Item, true);
		}

		private void SelectedDays_ItemRemoved(object sender, ItemEventArgs<DayOfWeek> e)
		{
			this.HandleDay(e.Item, false);
		}

		private void SelectedDays_Cleared(object sender, EventArgs e)
		{
			this.SelectAllCheck.Checked = true;
			this.SelectAllCheck.Checked = false;
		}



		private void HandleDay(DayOfWeek day, bool isChecked)
		{
			switch (day)
			{
				case DayOfWeek.Monday:
					this.MondayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Tuesday:
					this.TuesdayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Wednesday:
					this.WednesdayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Thursday:
					this.ThursdayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Friday:
					this.FridayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Saturday:
					this.SaturdayCheck.Checked = isChecked;
					break;
				case DayOfWeek.Sunday:
					this.SundayCheck.Checked = isChecked;
					break;
			}
		}

		private void HandleCheckbox(CheckBox checkbox, DayOfWeek day)
		{
			if (checkbox.Checked)
			{
				if(!this.SelectedDays.Contains(day))
				{
					this.SelectedDays.Add(day);
				}
			}
			else
			{
				if (this.SelectedDays.Contains(day))
				{
					this.SelectedDays.Remove(day);
				}
			}
		}

		private void MondayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.MondayCheck, DayOfWeek.Monday);
		}

		private void TuesdayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.TuesdayCheck, DayOfWeek.Tuesday);
		}

		private void WednesdayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.WednesdayCheck, DayOfWeek.Wednesday);
		}

		private void ThursdayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.ThursdayCheck, DayOfWeek.Thursday);
		}

		private void FridayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.FridayCheck, DayOfWeek.Friday);
		}

		private void SaturdayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.SaturdayCheck, DayOfWeek.Saturday);
		}

		private void SundayCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.HandleCheckbox(this.SundayCheck, DayOfWeek.Sunday);
		}

		private void SelectAllCheck_CheckedChanged(object sender, EventArgs e)
		{
			this.MondayCheck.Checked = this.SelectAllCheck.Checked;
			this.TuesdayCheck.Checked = this.SelectAllCheck.Checked;
			this.WednesdayCheck.Checked = this.SelectAllCheck.Checked;
			this.ThursdayCheck.Checked = this.SelectAllCheck.Checked;
			this.FridayCheck.Checked = this.SelectAllCheck.Checked;
			this.SaturdayCheck.Checked = this.SelectAllCheck.Checked;
			this.SundayCheck.Checked = this.SelectAllCheck.Checked;
		}

	}
}
